package com.yx.system.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.yx.common.annotation.LoginUser2;
import com.yx.common.utils.PageUtils;
import com.yx.common.utils.Query;
import com.yx.common.utils.R;
import com.yx.modules.api.entity.TbBookComment;
import com.yx.modules.api.entity.TbCommentDianzanRecord;
import com.yx.modules.api.entity.TbUser;
import com.yx.modules.api.service.TbBookCommentService;
import com.yx.modules.api.service.TbCommentDianzanRecordService;
import com.yx.modules.api.service.TbUserService;
import com.yx.system.entity.SysPrice;
import com.yx.system.entity.SysUser;
import com.yx.system.entity.TdBook;
import com.yx.system.service.TdBookService;
import com.yx.utils.HttpUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/sys/comment")
public class SysCommentController {

    @Autowired
    private TbBookCommentService tbBookCommentService;

    @Autowired
    private TbCommentDianzanRecordService tbCommentDianzanRecordService;

    @Autowired
    private TbUserService tbUserService;

    @Autowired
    private TdBookService tdBookService;


    /*
    *  查询作者的所有的图书
    */
    @RequestMapping("/list")
    @RequiresPermissions("sys:comment:list")
    public R list(@RequestParam Map<String, Object> params, @LoginUser2 SysUser sysUser){
        if (sysUser.getIdentity()==0){
            return R.error("作者才有权查看图书评论");
        }
        //查询对应的用户
        TbUser tbUser = tbUserService.findByMobile(sysUser.getUsername());
        if (tbUser.getIdentity()==0){
            return R.error("用户的身份不是作者");
        }
        //查询列表数据
        Query query = new Query(params);
        query.put("authorCode",tbUser.getUuid());
        Page<TdBook> pageUtil = new Page<TdBook>(query.getPage(), query.getLimit());
        Page<TdBook> page = tdBookService.queryPageList(pageUtil, query);
        return R.ok().put("page", page);
    }

    /*
    * 点击一本图书查看详细的图书评论
    */
    @RequestMapping("/getComment")
    @RequiresPermissions("sys:comment:getComment")
    public R getComment(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        List<TbBookComment> tbBookComments = tbBookCommentService.queryList(query);
        int total = tbBookCommentService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(tbBookComments, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    /*
    * 查看具体评论信息
    */
    @RequestMapping("/getCommentInfo")
    @RequiresPermissions("sys:comment:getCommentInfo")
    public R getComment(Integer id){
        TbBookComment tbBookComment = tbBookCommentService.queryObject(id);
        return R.ok().put("bookComment",tbBookComment);
    }

    /*
    * 作者删除评论（图书评论）
    */
    @RequestMapping("/delete")
    public R deleteComment(Integer id){
//        List<Integer> ids = HttpUtil.jsonToList(id,Integer.class);
//        for (int j = 0; j < ids.size(); j++) {
            //首先找到这个图书评论
            TbBookComment tbBookComment = tbBookCommentService.queryObject(id);
            //再找到之下的所有回复评论
            List<TbBookComment> replyBookComments = tbBookCommentService.findByReplyFristId(tbBookComment.getId());
            //找到评论的点赞记录
            List<TbCommentDianzanRecord> tbCommentDianzanRecords = tbCommentDianzanRecordService.findByCommentid(tbBookComment.getId());
            if (tbCommentDianzanRecords.size()!=0){
                //取出点赞记录id
                Integer[] dianzanIds = new Integer[tbCommentDianzanRecords.size()];
                //循环获取
                for (int i = 0; i < tbCommentDianzanRecords.size(); i++) {
                    dianzanIds[i] = tbCommentDianzanRecords.get(i).getId();
                }
                //删除点赞记录
                tbCommentDianzanRecordService.deleteBatch(dianzanIds);
            }
            //取出评论的id
            Integer[] commentIds = new Integer[replyBookComments.size()+1];
            //加入数组
            for (int i = 0; i < replyBookComments.size(); i++) {
                commentIds[i] = replyBookComments.get(i).getId();
            }
            commentIds[replyBookComments.size()] = tbBookComment.getId();
            //批量删除
            tbBookCommentService.deleteBatch(commentIds);

//        }
        return R.ok();
    }
}
